package com.ebt.m.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;


import com.ebt.m.R;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.data.bean.Brand;
import com.ebt.m.utils.UIHelper;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.wiki.SectionSearchHistoryBrand;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by andy.wang on 2017/12/1.
 */

public class LineBreakLayout extends IView implements IView.OnClickListner {


    private final static String TAG = "LineBreakLayout";
    /**
     * 所有标签
     */
    private List<Brand> lables;

    private int LEFT_RIGHT_SPACE; //dip
    private int ROW_SPACE;
    private int CONTENT_PADDING;

    public LineBreakLayout(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... args) {
        List<Brand> lables = (List<Brand>) args[0];
        if (this.lables == null) {
            this.lables = new ArrayList<>();
        }
        this.lables.clear();
        this.lables = lables;
        if (lables != null && lables.size() > 0) {
            for (final Brand brand : lables) {
                SectionSearchHistoryBrand historyBrand = new SectionSearchHistoryBrand(getContext());
                historyBrand.setOnClickViewListener(this);
                historyBrand.update(brand);
                addView(historyBrand);
            }
        }
    }

    public LineBreakLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LineBreakLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        CONTENT_PADDING = (int) getResources().getDimension(R.dimen.common_content_padding);
        LEFT_RIGHT_SPACE = UIHelper.dip2px(getContext(), 10);
        ROW_SPACE = UIHelper.dip2px(getContext(), 10);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);


        int height;
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            int childCount = getChildCount();
            if (childCount <= 0) {
                height = 0;
            } else {
                int row = 1;
                int widthSpace = width - 2 * CONTENT_PADDING;
                for (int i = 0; i < childCount; i++) {
                    View view = getChildAt(i);
                    int childW = view.getMeasuredWidth();
                    if (widthSpace >= childW) {
                        widthSpace -= childW;
                    } else {
                        row++;
                        widthSpace = width - 2 * CONTENT_PADDING - childW;
                    }
                    widthSpace -= LEFT_RIGHT_SPACE;
                }
                int childH = getChildAt(0).getMeasuredHeight();
                height = (childH * row) + ROW_SPACE * (row - 1);
            }
        }

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int row = 0;
        int right = CONTENT_PADDING;
        int botom;
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            int childW = childView.getMeasuredWidth();
            int childH = childView.getMeasuredHeight();
            right += childW;
            botom = row * (childH + ROW_SPACE) + childH;
            Logger.e("r:" + r);
            if (right > (r - LEFT_RIGHT_SPACE)) {
                row++;
                right = childW + CONTENT_PADDING;
                botom = row * (childH + ROW_SPACE) + childH;
            }
            childView.layout(right - childW, botom - childH, right, botom);
            right += LEFT_RIGHT_SPACE;
        }
    }

    @Override
    public void onClick(View view, Object... args) {
        if (mOnclickListner != null) {
            mOnclickListner.onClick(view, args);
        }
    }

    @Override
    public void onLongClick(View view, Object... args) {

    }
}
